import tkinter


def get_var_name(variable) -> str:
    globals_dict = globals()
    name_list = []
    for var_name in globals_dict:
        if globals_dict[var_name] is variable:
            name_list.append(var_name)
    for i in range(len(name_list)):
        if name_list[i] != 'my_var':
            return name_list[i]


def list_range(li) -> int or float:
    l_min = min(li)
    l_max = max(li)
    return (l_max - l_min) / 5




def axis_len(li, axis=True) -> list:
    axis_len = 0
    axis_len_list = []
    axis_len_list.append(axis_len)
    str_len = 0
    for i in range(len(li)):
        if type(li[i]) == type(str()):
            if len(li[i]) >= str_len:
                str_len = len(li[i])
        elif type(li[i]) != type(str()):
            string = str(li[i])
            if len(string) >= str_len:
                str_len = len(string)
    for i in range(len(li)):
        if axis:
            axis_len += 30 + str_len * 30
            axis_len_list.append(axis_len)
        elif axis != True:
            axis_len += 10 + 30
            axis_len_list.append(axis_len)
    axis_len += 30
    axis_len_list.append(axis_len)
    return axis_len_list




class Draw(object):
    def __init__(self, width=None, height=None, c_color='white'):
        self.__my_screen = tkinter.Tk()
        self.width = width
        self.height = height
        if (width is None) and (height is None):
            self.width = int(0.7 * self.__my_screen.winfo_screenwidth())
            self.height = int(0.7 * self.__my_screen.winfo_screenheight())
        elif (width is None) and (height is not None):
            self.height = int(0.7 * self.__my_screen.winfo_screenheight())
        elif (width is not None) and (height is None):
            self.width = int(0.7 * self.__my_screen.winfo_screenwidth())
        self.__my_canvas = tkinter.Canvas(self.__my_screen, bg=c_color, width=self.width, height=self.height)

    # 实线
    def line(self, start_x, start_y, end_x, end_y, fill='black', width=1):
        self.__my_canvas.create_line(start_x, start_y, end_x, end_y, width=width, fill=fill)

    # 虚线
    def dontted_line(self, start_x, start_y, end_x, end_y, width=1, fill='black', dash=100):
        self.__my_canvas.create_line(start_x, start_y, end_x, end_y, width=width, fill=fill, dash=dash)

    # 弧线
    def arc(self, start_x, start_y, end_x, end_y, start, extent, style, dash=(10, 2), outline='black', width=2):
        self.__my_canvas.create_arc(start_x, start_y, end_x, end_y, start, extent, style, dash=dash, outline=outline,
                                    width=width)

    # 多边形
    def polygon(self, point_list, outline='black', fill='black'):
        self.__my_canvas.create_polygon(point_list, outline=outline, fill=fill)

    # 矩形
    def rectangele(self, start_x, start_y, end_x, end_y, outline='black', fill='black'):
        self.__my_canvas.create_rectangle(start_x, start_y, end_x, end_y, outline=outline, fill=fill)

    # 圆形或者椭圆形
    def oval(self, start_x, start_y, end_x, end_y, outline='black', fill='black'):
        self.__my_canvas.create_oval(start_x, start_y, end_x, end_y, outline=outline, fill=fill)

    # 文本
    def text(self, start_x, start_y, text='Null', font=('Microsoft YaHei', 10)):
        if type(text) != type(str()):
            text = str(text)
        self.__my_canvas.create_text(start_x, start_y, text=text, font=font)

    # 散点图
    def sp(self, x_axis, y_axis, x_list, y_list):
        x_axis_len = axis_len(x_axis)
        y_axis_len = axis_len(y_axis, axis=False)


        #散点坐标
        plot = []
        y_len = y_axis_len.copy()
        y_len.pop()
        y_len.pop(0)
        for i in range(len(x_list)):
            x = 0
            y = 0
            #y坐标
            y = ((y_axis[-1] - y_list[i]) * y_len[-1] / y_axis[-1]) + 40
            x = x_axis_len[i + 1]
            plot.append([x, y])

        # y轴
        self.line(200, 200, 200, 200 + y_axis_len[-1])
        # x轴
        self.line(200, 200 + y_axis_len[-1], 200 + x_axis_len[-1], 200 + y_axis_len[-1])

        # y轴刻度
        for i in range(len(y_axis)):
            start_x = 0
            y_len = y_axis_len.copy()
            y_len.pop()
            y_len.pop(0)
            if type(y_axis[i]) != type(str()):
                start_x = len(str(y_axis[i])) * 10
            start_y = y_len[i]
            self.text(200 - start_x, 200 + start_y, text = y_axis[len(y_axis) - 1 - i])
            self.line(200, 200 + start_y, 205, 200 + start_y)

        # x轴刻度
        for i in range(len(x_axis)):
            start_y = 10
            self.text(200 + x_axis_len[i + 1], 200 + start_y + y_axis_len[-1], text=x_axis[i])
            self.line(200 + x_axis_len[i + 1], 200 + y_axis_len[-1], 200 + x_axis_len[i + 1], 200 + y_axis_len[-1] - 5)

        # 散点
        for i in range(len(plot)):
            self.oval(200 + plot[i][0] - 3, 200 + plot[i][1] - 3, 200 + plot[i][0] + 3, 200 + plot[i][1] + 3,outline='#4166f5', fill='#4166f5')
        
        
        
    #条形图
    def his(self, x_axis, y_axis, x_list, y_list):
        x_axis_len = axis_len(x_axis)
        y_axis_len = axis_len(y_axis, axis=False)


        #散点坐标
        plot = []
        y_len = y_axis_len.copy()
        y_len.pop()
        y_len.pop(0)
        for i in range(len(x_list)):
            x = 0
            y = 0
            #y坐标
            y = ((y_axis[-1] - y_list[i]) * y_len[-1] / y_axis[-1]) + 40
            x = x_axis_len[i + 1]
            plot.append([x, y])

        # y轴
        self.line(200, 200, 200, 200 + y_axis_len[-1])
        # x轴
        self.line(200, 200 + y_axis_len[-1], 200 + x_axis_len[-1], 200 + y_axis_len[-1])

        # y轴刻度
        for i in range(len(y_axis)):
            start_x = 0
            y_len = y_axis_len.copy()
            y_len.pop()
            y_len.pop(0)
            if type(y_axis[i]) != type(str()):
                start_x = len(str(y_axis[i])) * 10
            start_y = y_len[i]
            self.text(200 - start_x, 200 + start_y, text = y_axis[len(y_axis) - 1 - i])
            self.line(200, 200 + start_y, 205, 200 + start_y)

        # x轴刻度
        for i in range(len(x_axis)):
            start_y = 10
            self.text(200 + x_axis_len[i + 1], 200 + start_y + y_axis_len[-1], text=x_axis[i])
            self.line(200 + x_axis_len[i + 1], 200 + y_axis_len[-1], 200 + x_axis_len[i + 1], 200 + y_axis_len[-1] - 5)

        # 散点
        for i in range(len(plot)):
            self.rectangele(200 + plot[i][0] - 15, 200 + plot[i][1], 200 + plot[i][0] + 15, 200 + y_axis_len[-1] - 1,outline='#4166f5', fill='#4166f5')
        
        
        
    #折线图
    def lc(self, x_axis, y_axis, x_list, y_list):
        x_axis_len = axis_len(x_axis)
        y_axis_len = axis_len(y_axis, axis=False)


        #散点坐标
        plot = []
        y_len = y_axis_len.copy()
        y_len.pop()
        y_len.pop(0)
        for i in range(len(x_list)):
            x = 0
            y = 0
            #y坐标
            y = ((y_axis[-1] - y_list[i]) * y_len[-1] / y_axis[-1]) + 40
            x = x_axis_len[i + 1]
            plot.append([x, y])

        # y轴
        self.line(200, 200, 200, 200 + y_axis_len[-1])
        # x轴
        self.line(200, 200 + y_axis_len[-1], 200 + x_axis_len[-1], 200 + y_axis_len[-1])

        # y轴刻度
        for i in range(len(y_axis)):
            start_x = 0
            y_len = y_axis_len.copy()
            y_len.pop()
            y_len.pop(0)
            if type(y_axis[i]) != type(str()):
                start_x = len(str(y_axis[i])) * 10
            start_y = y_len[i]
            self.text(200 - start_x, 200 + start_y, text = y_axis[len(y_axis) - 1 - i])
            self.line(200, 200 + start_y, 205, 200 + start_y)

        # x轴刻度
        for i in range(len(x_axis)):
            start_y = 10
            self.text(200 + x_axis_len[i + 1], 200 + start_y + y_axis_len[-1], text=x_axis[i])
            self.line(200 + x_axis_len[i + 1], 200 + y_axis_len[-1], 200 + x_axis_len[i + 1], 200 + y_axis_len[-1] - 5)

        # 散点
        for i in range(len(plot)):
            self.oval(200 + plot[i][0] - 3, 200 + plot[i][1] - 3, 200 + plot[i][0] + 3, 200 + plot[i][1] + 3,outline='#4166f5', fill='#4166f5')
            
            if i >=1:
                self.line(200 + plot[i][0], 200 + plot[i][1], 200 + plot[i - 1][0], 200 + plot[i - 1][1],fill='black')
        

    # 显示
    def pack(self):
        self.__my_canvas.pack()
        self.__my_screen.mainloop()

    def __del__(self):
        print("{}析构函数已调用".format(get_var_name(self)))

